<?php

class Admin_CategoryController extends Zend_Controller_Action
{

    public function init()
    {
        // 动作助手缓存
        $this->_helper->cache(array('index', 'create'), array('allentries'));
    }

    /**
     * 分类索引页面
     * 
     * 使用动作堆栈助手，调用前台category的index()方法
     * 使用veiwRenderer动手禁止渲染自身的视图
     * 
     */
    public function homeAction()
    {
       
        $this->_helper->actionStack('index', 'category', 'default');
        $viewRenderer = $this->_helper->getHelper('viewRenderer');
        $viewRenderer->setNoRender(true);
    }

    /**
     * 创建分类
     * 
     */
    public function createAction()
    {
        $formCategory = new Admin_Form_Category();
        if($this->getRequest()->isPost()){
            if($formCategory->isValid($_POST)){
                $fid = $formCategory->getValue('fid');
                $name = $formCategory->getValue('name');
                $modelCategory = new Admin_Model_Category();
                $category = $modelCategory->createCategory($fid, $name);
                if($category == TRUE){
                    return $this->_redirect('/admin/category/home');
                }
            }
        }
        $this->view->formCategory = $formCategory;
    }

    /**
     * 更新分类
     * 
     */
    public function updateAction()
    {
        $id = $this->_request->getParam('id');
        $modelCategory = new Admin_Model_Category();
        $formCategory = new Admin_Form_Category();
        if($this->getRequest()->isPost()){
            if($formCategory->isValid($_POST)){
                $fid = $formCategory->getValue('fid');
                $name = $formCategory->getValue('name');
                $category  = $modelCategory->updateCategory($id, $fid, $name);
                if($category){
                    return $this->_redirect('/admin/category/home');
                }
            }
        }
        else{
            $currentCategory = $modelCategory->find($id)->current();
            $formCategory->populate($currentCategory->toArray());
            $this->view->formCategory = $formCategory;
        }
    }

    /**
     * 删除分类
     * 
     */
    public function deleteAction()
    {
        $id = $this->_request->getParam('id');
        $modelCategory = new Admin_Model_Category();
        $category = $modelCategory->deleteCategory($id);
        $this->_forward('home');
    }

    /**
     * 分类列表
     * 
     * 将查询结果使用Dojo_Data转换为JSON格式
     * 通过动用助手来提供响应
     */
    public function listAction()
    {
        $modelCatetory = new Ms_Model_Category();
        $cid = $this->_request->getParam('cid');
        $fid = $this->_request->getParam('fid');
        if ($cid) {
            $fid = $modelCatetory->getFirstFidById($cid);
        }
        
        if ($fid){
            $where = array('fid'=>$fid);
        } else{
            $where = array('fid'=>0);
        }
        $categories = $modelCatetory->getCategories($where);
        $data = new Zend_Dojo_Data('id', $categories);
        $this->_helper->autoCompleteDojo($data);
    }
}









